from django.db import models
import time
import hashlib
from random import Random


def randomNumber(length=20):
    string = ''
    chars = '0123456789'
    random = Random()
    for i in range(length):
        string += chars[random.randint(0, len(chars) - 1)]
    return string


def getLocalToken():
    timeStamp = time.time()
    localTime = time.localtime(timeStamp)
    strTime = time.strftime("%Y%m%d%H", localTime)
    str = randomNumber(30)
    hl = hashlib.md5()
    hl.update(str.encode(encoding='utf-8'))
    return hl.hexdigest()[0:20]


# Create your models here.
# 信息
class WarZone(models.Model):
    WARZONESTATE = (
        ('0', u'已关闭'),
        ('1', u'已开启'),
    )
    name = models.CharField(
        verbose_name=u'战区名称',
        default=u'',
        max_length=20
    )
    warzoneState = models.CharField(
        verbose_name=u'新增战区',
        choices=WARZONESTATE,
        max_length=50,
        default=1,
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    def __unicode__(self):
        return self.createTime

    class Meta:
        ordering = ('createTime',)


class Company(models.Model):
    COMPANYSTATE = (
        ('0', u'已关闭'),
        ('1', u'已开启'),
    )
    name = models.CharField(
        verbose_name=u'单位名称',
        default=u'',
        max_length=20
    )
    warZone = models.ForeignKey(
        WarZone,
        verbose_name=u'所属战区',
        on_delete=models.CASCADE,
        related_name='user_warZone',
        default=''
    )
    companyState = models.CharField(
        verbose_name=u'新增单位',
        choices=COMPANYSTATE,
        max_length=50,
        default=1,
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    def __unicode__(self):
        return self.createTime

    class Meta:
        ordering = ('createTime',)


class User(models.Model):
    USERTYPE = (
        ('1', u'开发人员'),
        ('2', u'仓库管理人员'),
        ('3', u'使用人员'),
    )
    company = models.ForeignKey(
        Company,
        verbose_name=u'所属单位',
        on_delete=models.CASCADE,
        related_name='user_Company',
        default=''
    )

    warZone = models.ForeignKey(
        WarZone,
        verbose_name=u'所属战区',
        on_delete=models.CASCADE,
        related_name='user_WarZone',
        default=''
    )
    username = models.CharField(
        max_length=30,
        verbose_name=u'用户名',
        default=u'',
    )
    name = models.CharField(
        max_length=30,
        verbose_name=u'姓名',
        default=u'',
    )
    userId = models.CharField(
        max_length=100,
        verbose_name=u'用户Id',
        default=u'',
    )
    userToken = models.CharField(
        max_length=100,
        verbose_name=u'用户token',
        default=u'',
    )
    userType = models.CharField(
        choices=USERTYPE,
        max_length=50,
        verbose_name=u"用户类型",
    )
    phone = models.CharField(
        verbose_name=u'电话',
        max_length=20,
        default=u''
    )
    refreshToken = models.CharField(
        max_length=100,
        verbose_name=u'freshToken',
        default=u'',
    )
    sex = models.CharField(
        max_length=10,
        verbose_name=u"性别",
        default=u""
    )
    borthTime = models.CharField(
        max_length=10,
        verbose_name=u"borthTime",
        default=u""
    )
    cardId = models.CharField(
        max_length=30,
        verbose_name=u"身份证号",
        default=u""
    )
    deptId = models.CharField(
        max_length=10,
        verbose_name=u"所属系统ID",
        default=u""
    )
    email = models.CharField(
        max_length=20,
        verbose_name=u"email",
        default=u""
    )
    nation = models.CharField(
        max_length=10,
        verbose_name=u"民族",
        default=u""
    )
    position = models.CharField(
        max_length=10,
        verbose_name=u"职位",
        default=u""
    )
    politivalStatus = models.CharField(
        max_length=20,
        verbose_name=u"政治面貌",
        default=u""
    )
    postLevel = models.CharField(
        max_length=20,
        verbose_name=u"职位等级",
        default=u""
    )
    password = models.CharField(
        max_length=50,
        verbose_name=u"passwd",
        default=u""
    )
    isSupervisor = models.BooleanField(
        verbose_name=u'是否超管',
        default=False,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    def __str__(self):
        return self.createTime

    class Meta:
        ordering = ('createTime',)


class VSession(models.Model):
    token = models.CharField(
        max_length=100,
        verbose_name=u'临时秘钥',
        db_index=True,  # 建立索引加快查找,
        unique=True
    )
    user = models.ForeignKey(
        User,
        verbose_name=u"用户信息",
        on_delete=models.CASCADE,
    )
    lastVisiteTime = models.DateTimeField(
        auto_now=True,
        verbose_name=u"最晚访问时间",
    )
    duration = models.CharField(
        max_length=30,
        verbose_name=u'登陆时长',
        default=u'',
        null=True
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    def __unicode__(self):
        return self.user

    class Meta:
        ordering = ('createTime',)


class FileStore(models.Model):
    FILETYPE = (
        (0, u"material"),
        (1, u"verification"),
        (2, u'file'),
        (3, u'logo'),
        (4, u'cover'),
        (5, u'tmpFile'),
        (6, u'QAFile')
    )
    owner = models.ForeignKey(
        User,
        verbose_name=u"上传用户",
        on_delete=models.DO_NOTHING,
    )  # 增加断点续传：百分比  文件的md5是否是同一个文件，判断多少
    fileName = models.CharField(
        max_length=50,
        verbose_name=u'文件名称',
        default=None,
    )
    filePath = models.CharField(
        verbose_name=u'文件路径',
        default=None,
        max_length=100
    )
    fileType = models.IntegerField(
        choices=FILETYPE,
        default=1,
        verbose_name=u"文件类型",
    )
    size = models.CharField(
        verbose_name=u'文件大小',
        default='0',
        max_length=20
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    uniqueFile = models.CharField(
        verbose_name=u"唯一标文件",
        max_length=20,
        default=getLocalToken(),
    )
    dfsPath = models.CharField(
        verbose_name=u"分布式存储路径",
        max_length=200,
        default=u""
    )

    def __unicode__(self):
        return self.createTime

    class Meta:
        ordering = ('createTime',)


# class UserIP(models.Model):
#     user = models.ForeignKey(
#         User,
#         verbose_name=u"用户信息",
#         on_delete=models.CASCADE,
#     )
#     userIP = models.GenericIPAddressField(
#         verbose_name=u'用户IP地址',
#         default=u'',
#     )
#     lastVisiteTime = models.DateTimeField(
#         auto_now=True,
#         verbose_name=u"最晚访问时间",
#     )
#     isActive = models.BooleanField(
#         verbose_name=u'是否活跃',
#         default=True,
#     )
#     createTime = models.DateTimeField(
#         auto_now_add=True,
#         verbose_name=u"创建时间",
#     )

#     def __unicode__(self):
#         return self.user

#     class Meta:
#         ordering = ('createTime',)


class IPSystem(models.Model):
    IPSTATE = (
        (0, u"已开启"),
        (1, u"已禁用"),
    )
    user = models.ForeignKey(
        User,
        verbose_name=u"用户信息",
        on_delete=models.CASCADE,
    )
    userIP = models.GenericIPAddressField(
        verbose_name=u'用户IP地址',
        default=u'',
    )
    IPState = models.IntegerField(
        choices=IPSTATE,
        default=0,
        verbose_name=u"IP状态",
    )
    duration = models.CharField(
        max_length=30,
        verbose_name=u'登陆时长',
        default=u'',
        null=True
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )
    isForbid = models.BooleanField(
        verbose_name=u'是否禁用',
        default=False,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )

    def __unicode__(self):
        return self.user

    class Meta:
        ordering = ('createTime',)
